<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2019-07-14
 * Time: 12:06
 */

namespace app\common\service;


use app\common\exception\ErrorException;
use library\File;
use think\Controller;

class Files extends Controller
{
    //上传文件
    public function upload()
    {
        $file = $this->request->file('file');
        if (!$file->checkExt(['mp4', 'avi', 'wmv', 'webm', 'png', 'gif', 'jpg', 'jpeg', 'bmp', 'png'])) {
            throw new ErrorException(4005, 200, '上传失败,文件格式错误');
        }
        $tmp_name = $file->getInfo('tmp_name');
        $fileName = md5_file($tmp_name);
        $ext = strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION));
        $fileName = $fileName . '.' . $ext;
        $url = File::url($fileName);
        if($url){
            return $url;
        }else{
            $res = File::save($fileName,file_get_contents($tmp_name));
            if($res){
                return $res['url'];
            }else{
                throw  new ErrorException(4006, 200, '上传失败');
            }
        }

    }
}